var lj = {
    dataOptions:{
        pageNumber:1,
        onDblClickCell:function (index, field) {
            if (field == "mc") {
                var row = $(this).datagrid("getSelected");
                if (row.sfdj == null || row.sfdj == '是')
                    lj.showWindow({"lj.id":row.id});
            }
        },
        onLoadSuccess:function () {
            $(this).datagrid('selectRow', 0);
            var query = $.data(this, 'datagrid').options.queryParams;
        },
        loadFilter:function (data) {
            $.each(data.rows, function () {
                if (this.zt) {
                    this.zt = this.zt == 'ysp' ? "已审批" : this.zt == 'ytj' ? "已提交" : "已保存";
                }
            })
            return data;
        },
        onClickRow:function (rowIndex, rowData) {
            if (rowData) {
                if (rowData.sfdj == null || rowData.sfdj == '是') {
                    $("#btn_lj_start").text('解冻');
                    $("#btn_lj_start").removeClass('stop').addClass('start');
                    $('#btn_lj_start').unbind('click');
                    $('#btn_lj_start').bind('click', function () {
                        changeLjState($.webPath() + '/organ/ljwh/start_lj', '确认解冻此路径？');
                    });
                } else {
                    $("#btn_lj_start").text('冻结');
                    $("#btn_lj_start").removeClass('start').addClass('stop');
                    $('#btn_lj_start').unbind('click');
                    $('#btn_lj_start').bind('click', function () {
                        changeLjState($.webPath() + '/organ/ljwh/stop_lj', '确认冻结此路径？');
                    });
                }
            }
            lj.setBtnStatus(rowData);
        },
        onSelect:function (rowIndex, rowData) {
            if (rowData) {
                if (rowData.sfdj == null || rowData.sfdj == '是') {
                    $("#btn_lj_start").text('解冻');
                    $("#btn_lj_start").removeClass('stop').addClass('start');
                    $('#btn_lj_start').unbind('click');
                    $('#btn_lj_start').bind('click', function () {
                        changeLjState($.webPath() + '/organ/ljwh/start_lj', '确认解冻此路径？');
                    });
                } else {
                    $("#btn_lj_start").text('冻结');
                    $("#btn_lj_start").removeClass('start').addClass('stop');
                    $('#btn_lj_start').unbind('click');
                    $('#btn_lj_start').bind('click', function () {
                        changeLjState($.webPath() + '/organ/ljwh/stop_lj', '确认冻结此路径？');
                    });
                }
            }
            lj.setBtnStatus(rowData);
        },
        rowStyler:function (index, row) {
            if (row.sfdj == '是') {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        },
        columns:[
            [    {field:'checked', checkbox:true},
                {field:'mc', title:'名称', align:'center', width:12, formatter:ljwhCell},
                {field:'jyjg_jc', title:'运营商', align:'center', width:4},
                {field:'sfdj', title:'是否冻结', align:'center', width:3},
                {field:'zt', title:'状态', align:'center', width:2, formatter:formatLjSp}
            ]
        ]
    },
    initTree:function () {
        $("#tree_lj").ljtreegrid(1, $("#lj_zggsId").val(), {
            onLoadSuccess:function () {
                $(this).tree("select", $(this).tree("getRoot").target);
                $("#query_lj_jyjg").val($(this).tree("getRoot").id);
                $("#query_lj_jyjg").combotree("setValue", $(this).tree("getRoot").id);
                setTimeout(function () {
                    $("#btn_lj_search").click();
                }, 100);
            },
            onClick:function () {
                $("#query_lj_jyjg").combotree("setValue", $(this).tree("getSelected").id);
                $("#btn_lj_search").click();
            }
        });
    },
    initDatagrid:function () {
        $('#grid_lj').datagrid(lj.dataOptions);
    },
    showWindow:function (ljdata) {
        var tit = ljdata["lj.id"] ? "修改" : "添加";
        var win = $.window({
            title:tit,
            iconCls:'icon-add2',
            width:680,
            height:450,
            cache:false,
            url:$.webPath() + "/organ/ljwh/ljwh_content",
            data:ljdata,
            buttons:[
                {
                    text:'保存',
                    handler:function () {
                        $.messager.progress();
                        $('#lj_wlyForm').form('submit', {
                            url:$.webPath() + "/organ/ljwh/lj_save",
                            onSubmit:function (param) {
                                var isValid = $(this).form('validate');
                                if (!isValid) {
                                    $.messager.progress('close');
                                } else {
                                    var fj = $("#lj-fileQueue input[name='lj.fjList.id']").map(function () {
                                        return $(this).val();
                                    });
                                    for (var i = 0; i < fj.length; i++) {
                                        param['lj.fjList[' + i + '].id'] = fj[i];
                                    }
                                }
                                return isValid;
                            },
                            success:function (data) {
                                $.messager.progress('close');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(win).window("close");
                                    $('#grid_lj').datagrid('reload');
                                    $('#layout_ljwh').layout('panel', 'center').panel('refresh');
                                } else
                                    $.messager.alert('提示', json.message, 'info');
                            }
                        });
                    }
                }
            ],
            onBeforeClose:function () {
                window.parent.destroy('lj-uploadify');
            }
        });
    },
    initBtn:function () {
        //新增
        $("#btn_lj_add").click(function () {
            var data = {"lj.jyjg.id":$("#tree_lj").tree("getSelected").id};
            lj.showWindow(data);
        });
        //删除
        $("#btn_lj_del").click(function () {
            var row = $('#grid_lj').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否删除此路径?', function (r) {
                    if (r) {
                        var index = $('#grid_lj').datagrid('getRowIndex', row);
                        $.post($.webPath() + '/organ/ljwh/lj_delete', {ljId:row.id}, function (data) {
                            data = $.parseJSON(data);
                            if (data.statusCode == '200') {
                                $('#grid_lj').datagrid('deleteRow', index);
                                lj.setBtnStatus(row);
                            } else {
                                $.messager.alert("提示", data.message, "info");
                            }

                        });
                    }
                });
            } else {
                $.messager.alert("提示", "请选择要删除的工程", "info");
            }
        });
        //修改
        $("#btn_lj_mod").click(function () {
            var row = $("#grid_lj").datagrid("getSelected");
            lj.showWindow({"lj.id":row.id});
        });

        $("#btn_lj_commit").click(function () {
            var row = $('#grid_lj').datagrid('getSelected');
            $.messager.confirm('确认', '是否确认提交此路径', function (r) {
                if (r) {
                    $.post($.webPath() + '/organ/ljwh/lj_commit', {ljId:row.id}, function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.statusCode == '200') {
                            $('#grid_lj').datagrid('reload');
                            $('#layout_ljwh').layout('panel', 'center').panel('refresh');
                            $.messager.show({title:'提示框', msg:obj.message, timeout:3000, showType:'slide'});
                        } else
                            $.messager.alert('提示', obj.message, 'info');
                    });
                }
            });
        })

        //查询
        $("#btn_lj_search").click(function () {
            $('#grid_lj').datagrid({url:$.webPath() + "/organ/ljwh/ljwhList", queryParams:lj.getQuery()});
        });
    },
    initQuery:function () {
        $("#query_lj_jyjg").ljgsjgtree(1, $("#lj_zggsId").val());
        $("#query_lj_qy").qyjgzcombobox();
    },
    setBtnStatus:function (row) {
        if (row) {
            if (row.zt == '已保存') {
                $("#btn_lj_mod").show();
                $("#btn_lj_del").show();
                $("#btn_lj_commit").show();
                $("#btn_lj_start").hide();
            } else {
                $("#btn_lj_mod").hide();
                $("#btn_lj_del").hide();
                $("#btn_lj_commit").hide();
                $("#btn_lj_start").hide();
            }

            if (row.zt == '已审批') {
                if (row.sfdj == "否") {
                    $("#btn_lj_mod").hide();
                    $("#btn_lj_del").hide();
                    $("#btn_lj_commit").hide();
                } else {
                    $("#btn_lj_mod").show();
                    $("#btn_lj_del").show();
                    $("#btn_lj_commit").hide();
                }
                $("#btn_lj_start").show();
            }
        } else {
            $("#btn_lj_mod").hide();
            $("#btn_lj_del").hide();
            $("#btn_lj_commit").hide();
            $("#btn_lj_start").hide();
        }
    },
    alwayShowDetail:function (rowData) {
        $('#layout_ljwh').layout('remove', 'center');
        $('#layout_ljwh').layout('add', {
            region:'center',
            split:true,
            href:$.webPath() + '/organ/ljwh/ljwh_detail?ljId=' + rowData.id
        });
    },
    showDetail:function (rowData) {
        if (rowData && $("#ljwh_detail_id").val() != rowData.id) {
            lj.alwayShowDetail(rowData);
        } else {
            $('#layout_ljwh').layout('remove', 'center');
        }
    },
    getQuery:function () {
        return {
            "queryLj.jyjg":$("#query_lj_jyjg").combotree("tree").tree("getSelected").id,
            "queryLj.qy":$("[name='querylj.qy']").val()
        }
    }
}

$(function () {
    //树-加载
    lj.initTree();
    //列表-加载
    lj.initDatagrid();
    //按钮事件-加载
    lj.initBtn();
    //按钮状态
    lj.setBtnStatus();
    //查询条件-加载
    lj.initQuery();
});

//改变状态，冻结解冻
function changeLjState(url, message) {
    var ljId = $("#grid_lj").datagrid("getSelected").id;
    if (ljId) {
        $.messager.confirm("确认", message, function (r) {
            if (r) {
                $.ajax({
                    url:url + "?ljId=" + ljId,
                    success:function (data) {
                        if (data.statusCode == '200') {
                            $('#grid_lj').datagrid('reload');
                            $('#layout_ljwh').layout('panel', 'center').panel('refresh');
                            $.messager.show({title:'提示框', msg:data.message, timeout:3000, showType:'slide'});
                        } else {
                            $.messager.alert('提示', data.message, 'info');
                        }
                    }
                });
            }
        })
    }
}

//改变状态，可修改
function changeLjUpdate(url, message) {
    var ljId = $("#grid_lj").datagrid("getSelected").id;
    if (ljId) {
        $.messager.confirm("确认", message, function (r) {
            if (r) {
                $.ajax({
                    url:url + "?ljId=" + ljId,
                    success:function (data) {
                        if (data.statusCode == '200') {
                            $('#grid_lj').datagrid('reload');
                            $.messager.show({
                                title:'提示框',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $.messager.alert('提示', data.message, 'info');
                        }
                    }
                });
            }
        })
    }
}

function formatLjSp(val, row) {
    if (!row.processInstanceId) {
        return val;
    } else {
        return '<a href="javascript:viewLjFlowPicture();" style="color: red">' + val + '</a>';
    }
}

function viewLjFlowPicture() {
    var row = $('#grid_lj').datagrid('getSelected');
    $.window({
        title:"流程历史",
        iconCls:'icon-add2',
        url:$.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(row.processInstanceId)) + "&businessNo=" + row.mc,
        width:650,
        height:500,
        closed:true,
        modal:true,
        isMax:true
    });
}
